// pages/home/home.js
import { request } from "../../utils/index.js";
const app = getApp()
var animation;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: getApp().globalData.baseUrl + "/uploads/",
    bannerList:[], // 轮播图
    noticeList:[], // 公告通知
    signList:[], // 签到列表
    lotteryUserList:[], // 抽奖排名
    lotteryGoodsList:[], // 商品列表
    lottery_num:0, // 抽奖次数
    todayIsSign:0, // 是否签到 0 未签到 1已签到
    // weeks:['周一','周二','周三','周四','周五','周六','周日'],
    rotateSwitch:true,
    prizeList: ['5元', '10元', '15元', '20元', '5元', '10元', '15元', '20元'],
    color: ["#8DCBFB", "#FFFFFF","#8DCBFB", "#FFFFFF","#8DCBFB", "#FFFFFF","#8DCBFB", "#FFFFFF"],//扇形的背景颜色交替；
    rotateInfo:{},
    show: false,
    page: 1, // 分页数
    articleList:[], // 精选栏目列表
   stepInfo:[
      {
        status:1,
        pro_name:'周一'
      },
      {
        status:1,
        pro_name:'周二'
      },
      {
        status:0,
        pro_name:'周三'
      },
      {
        status:0,
        pro_name:'周四'
      },
      {
        status:0,
        pro_name:'周五'
      },
      {
        status:0,
        pro_name:'周六'
      },
      {
        status:0,
        pro_name:'周日'
      },
    ]
  },

  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  
  },


  /**
   * 获取首页数据
   */
  getIndex () {
    var that = this;
    request({ url: "/api/index/index"})
    .then(result => {
      that.setData({
        bannerList:result.data.banner,
        noticeList: result.data.notice,
        signList:result.data.signList,
        lotteryUserList:result.data.lotteryUser,
        lotteryGoodsList:result.data.lotteryGoodsList,
        lottery_num:result.data.lottery_num,
        todayIsSign:result.data.todayIsSign
      })
      that.initAwardList(that.data.lotteryGoodsList);
    })
  },

  

  /**
   * 获取精选栏目数据
   */
  getArticle(){
    var that = this;
    request({ url: "/api/article/index" ,data:{page:that.data.page}})
    .then(result => {
      that.setData({
        articleList:result.data.list
      })
    })
  },

  // 公告详情
  onNotice(event){
    let str=JSON.stringify(event.currentTarget.dataset.value);
    wx.navigateTo({
      url: '/pages/notice/notice?jsonStr='+encodeURIComponent(str), 
    })
  },

  /**
   * 签到
   */
  onSign(){

    var token = wx.getStorageSync("token");
    if (!token) {
      return wx.navigateTo({
        url: '/pages/login/login',
      })
    }

    if (this.data.todayIsSign == 0) {
      request({ url: "/api/sign/addSign" })
      .then(result => {
        console.log(result)
        wx.showToast({
          title: result.msg,
          icon: 'none',
          duration:2000
        });
        this.getIndex();
      })
    }
  },


  /**
   * 初始化抽奖
   */
  initAwardList: function(list) {
    // 绘制转盘
    var awardsList = [];
    var angle = 360 / list.length;
    for (var i = 0; i < list.length; i++) {
      let item = list[i];
      awardsList.push({
        degree: {
          item: `${i * angle}deg`,
          line: `${(i + 0.5) * angle}deg`,
          bg_1: `${(i - 0.5) * angle + 90}deg`,
          bg_2: `${angle - 90}deg`
        },
        // award: list[i],
        award: item.name,
        color: this.data.color[i]
      })
    }
    this.setData({
      awardsList: awardsList
    });
  },

  /**
   * 开始抽奖
   */
  getLottery: function() {

    var token = wx.getStorageSync("token");
    if (!token) {
      return wx.navigateTo({
        url: '/pages/login/login',
      })
    }

    let that=this
    request({ url: "/api/lottery/lottery" })
    .then(result => {
      console.log(result)
      that.setData({
        rotateInfo:result
      })
      if(result.data.id == "5") { //5元
           /// 获奖序号
        var awardIndex = 0;
      }else if (result.data.id == "6") { //10元
          /// 获奖序号
       var awardIndex = 1;
      }else if (result.data.id == "7") { //15元
          /// 获奖序号
        var awardIndex = 2;
      }else if (result.data.id == "8") { //20元
          /// 获奖序号
        var awardIndex = 3;
      }else if (result.data.id == "9") { //5元
          /// 获奖序号
        var awardIndex = 4;
      }else if (result.data.id == "10"){ //10元
          /// 获奖序号
        var awardIndex = 5;
      }else if (result.data.id == "11"){ //10元
        /// 获奖序号
       var awardIndex = 6;
      }else if (result.data.id == "12"){ //10元
        /// 获奖序号
        var awardIndex = 7;
      }
    
      /// 转动时间
      let duration = 4000;
      /// 转动圈数
      let runNum = 6;

      // 旋转抽奖
      this.runDegs = this.runDegs || 0;
      this.runDegs = this.runDegs + (360 - this.runDegs % 360) + (360 * runNum - awardIndex * (360 / this.data.awardsList.length));

      /// 开始转动
      this.startRun(awardIndex, duration, this.runDegs);
    })

   
  },

  /**
   * 开始转动
   */
  startRun: function(awardIndex, duration, runDegs) {
    /// 动画
    var animation = wx.createAnimation({
      duration: duration,
      timingFunction: 'ease'
    })
    animation.rotate(runDegs).step();
    this.setData({
      animationData: animation.export()
    })

    // 中奖提示
    setTimeout(() => {
      this.stopRun(awardIndex);
    }, duration + 300)
  },

  /**
   * 结束转动
   */
  stopRun: function(awardIndex) {
    console.log('抽中了奖品: ' + awardIndex)
    let that=this
    that.setData({ show: true });
    wx.showToast({
      title: '抽中了奖品:'+that.rotateInfo.name,
      icon: 'none',
      duration:2000
      });
  },

  // 中奖记录
  toRecords(){

    var token = wx.getStorageSync("token");
    if (!token) {
      return wx.navigateTo({
        url: '/pages/login/login',
      })
    }

    wx.navigateTo({
      url: '/pages/lotteryResult/lotteryResult',
    })
  },

  // 点击精选栏目
  onItmeClick(item){
    let id = item.currentTarget.dataset.value.id;
    wx.navigateTo({
      url: '/pages/articleDetail/articleDetail?id='+id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 首页数据
    this.getIndex()
    // 精选栏目
    this.getArticle()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})